package ControlLayer;
import ModelLayer.*;
import java.text.DateFormat;
import java.util.Date;
import java.util.ArrayList;
/**
 * Write a description of class OrderController here.
 * 
 * @author Group1
 * @version 2011-06-08
 */
public class OrderController
{
    private OrderContainer orderContainer;
    private ItemController itemController;
    private String toDaysDate;
    private PersonContainer personContainer;   
    private PersonController personController;
    /**
     * Constructor for objects of class OrderController
     */
    public OrderController()
    {
        orderContainer = OrderContainer.getInstance();
        itemController = new ItemController();
        toDaysDate ="";
        personController = new PersonController();
        personContainer = PersonContainer.getInstance();        
    }

    /**
     * Creates an date with time
     * return toDaysDate
     */    
    public String timeAndDate()
    {
        toDaysDate = DateFormat.getDateInstance().format(new Date());
        return toDaysDate;
    }

    /**
     * Find a order
     * @param id Id of the order to be found
     * @param throws Exception Throws an error further up in the system
     * @return Returns a Order
     */    
    public Order findOrder(int id) throws Exception
    {
        Order order = orderContainer.findOrder(id);
        if (order == null)
        {
            throw new Exception("No order found");
        }
        return order;
    }
    
    public ArrayList<Order> findOrderByName(String searchName) throws Exception
    {
        if(orderContainer.findOrderByName(searchName).isEmpty())
        {
            throw new Exception("No order is found by that customer name");
        }
        return orderContainer.findOrderByName(searchName);
    }
    
    public void createOrder(String customersName)
    {
        setLastCreatedOrderId();
        Order o = new Order(getLastCreatedOrderId(), timeAndDate(), customersName);
        o.setStatus("created");
        orderContainer.createOrder(o);
    }
    
    public void deleteOrder(int id) throws Exception
    {
        if(orderContainer.findOrder(id) == null)
        {
            throw new Exception("order not found");
        }
        else
        {
            orderContainer.deleteOrder(id);
        }
    }
    
    public Item findItemByBarcode(String searchBarcode)
    {
        return itemController.findItemByBarcode(searchBarcode);
    }
    
    public Item findItemByItemNumber(String searchItemNumber)
    {
        return itemController.findItemByItemNumber(searchItemNumber);
    }
    
    public void createPartOrderAndAddToOrder(int amount, String searchBarcode)
    {
        PartOrder partOrder = new PartOrder(amount, findItemByBarcode(searchBarcode));
        
        Order order = orderContainer.findOrder(getLastCreatedOrderId());
        
        double partOrderPrice = amount * partOrder.getItem().getSellingPrice();
        partOrder.setPartOrderPrice(partOrderPrice);
        
        double partOrderPriceNoVAT = partOrderPrice - (partOrderPrice * order.getVAT());
        partOrder.setPartOrderPriceNoVAT(partOrderPriceNoVAT);
        
        order.addPartOrder(partOrder);
    }
    
    public void calculateOrderPrice()
    {
        try
        {
            Order order = findOrder(getLastCreatedOrderId());
            double totalOrderPrice = 0;
            double totalOrderPriceNoVAT = 0;
            double VATPrice = 0;
            for(PartOrder partOrder : order.getPartOrders())
            {
                totalOrderPrice += partOrder.getPartOrderPrice();
            }
            order.setTotalPrice(totalOrderPrice);
        
            for(PartOrder partOrder : order.getPartOrders())
            {
                totalOrderPriceNoVAT += partOrder.getPartOrderPriceNoVAT();
            }
            order.setWithoutVAT(totalOrderPriceNoVAT);
            
            VATPrice = totalOrderPrice - totalOrderPriceNoVAT;
            order.setVATPrice(VATPrice);
        }
        catch (Exception e)
        {
            System.out.println(e.getMessage());
        }
    }
    
    public void deletePartOrderFromOrder()
    {
    }
    
    public ArrayList<Order> listAllOrder()
    {
        return orderContainer.listAllOrder();
    }
    
    public int getLastCreatedOrderId()
    {
        return orderContainer.getLastCreatedOrderId();  
    }
    
    public void setLastCreatedOrderId()
    {
        orderContainer.setLastCreatedOrderId();
    }
    
    public void setBuyerInfo(int orderId, int phoneNumber) throws Exception
    {
        Order newOrder = orderContainer.findOrder(orderId);
        Person newPerson = personContainer.findPerson(phoneNumber);
        if(newPerson == null)
        {
            throw new Exception("Customer not found");
        }
        newOrder.setPerson(newPerson);
    } 
    
    public Person getBuyerInfo(int id)
    {
        
        Order order = orderContainer.findOrder(id);
        Person person = order.getPerson();
        Person newObj = null;
        String personType = personController.checkObjectType(person);
        
        if(personType.equals("PrivateCustomer"))
        {
            newObj = (RegisteredCustomer)person; 
        }
        if(personType.equals("BusinessCustomer"))
        {
            newObj = (RegisteredCustomer)person;
        }
        if(personType.equals("WarehouseEmployee"))
        {
            newObj = (Employee)person;
        }
        if(personType.equals("Manager"))
        {
            newObj = (Employee)person;
        }
        if(personType.equals("Salesman"))
        {
            newObj = (Employee)person;
        }
        
        return newObj;
    }
//     Skal bruges til at finde telefonnummeret p� Buyer s� man kan vise det n�r man s�ger p� en ordre.
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
}